
/* globals svgEditor */
import { isValidUnit } from '../../common/units.js'
import imagePropertiesDialogHTML from './newalter.html'

const template = document.createElement('template')
template.innerHTML = imagePropertiesDialogHTML
/**
 * @class SeImgPropDialog
 */
export class SeSave extends HTMLElement {
  /**
    * @function constructor
    */
  constructor() {
    super()
    // create the shadowDom and insert the template
    this.eventlisten = false
    this._shadowRoot = this.attachShadow({ mode: 'open' })
    this._shadowRoot.append(template.content.cloneNode(true))
    this.$dialog = this._shadowRoot.querySelector('#svg_createment')
    this.$donot_save_close = this._shadowRoot.querySelector('#donot_save_close')
    this.$cancel = this._shadowRoot.querySelector('#cancel')
    this.$save = this._shadowRoot.querySelector('#save')
    


  }

  /**
   * @function init
   * @param {any} name
   * @returns {void}
   */
  init(i18next) {
    this.setAttribute('common-ok', i18next.t('common.save'))
    this.setAttribute('common-cancel', i18next.t('common.cancel'))
    this.setAttribute('donot_save_close', i18next.t('common.not_close_save'))

  }

  /**
   * @function observedAttributes
   * @returns {any} observed
   */
  static get observedAttributes() {
    return ['dialog', 'common-ok', 'common-cancel','donot_save_close']
  }

  /**
   * @function attributeChangedCallback
   * @param {string} name
   * @param {string} oldValue
   * @param {string} newValue
   * @returns {void}
   */
  attributeChangedCallback(name, oldValue, newValue) {
    if (oldValue === newValue) return
    switch (name) {
      case 'dialog':
        if (newValue === 'open') {
          this.$dialog.open()
        } else {
          this.$dialog.close()
        }
        break
        case 'common-ok':
          this.$save.textContent = newValue
          break
        case 'common-cancel':
          this.$cancel.textContent = newValue
          break
          case 'donot_save_close':
          this.$donot_save_close.textContent = newValue
          break
      default:
        super.attributeChangedCallback(name, oldValue, newValue)
        break
    }
  }

  /**
   * @function get
   * @returns {any}
   */


  /**
   * @function get
   * @returns {any}
   */
  get dialog() {
    return this.getAttribute('dialog')
  }

  /**
   * @function set
   * @returns {void}
   */
  set dialog(value) {
    this.setAttribute('dialog', value)
  }

  /**
   * @function connectedCallback
   * @returns {void}
   */
  connectedCallback() {
    const donSaveClose = () => {
      const closeEvent = new CustomEvent('change', {
        detail: {
          dialog: 'donot_save_close',
        }
      })
      this.dispatchEvent(closeEvent)
    }
    const cancels = () => {
      const closeEvent = new CustomEvent('change', {
        detail: {
          dialog: 'closed'
        }
      })
      this.dispatchEvent(closeEvent)
    }

    const saves = () => {
      const closeEvent = new CustomEvent('change', {
        detail: {
          dialog: 'saves'
        }
      })
      this.dispatchEvent(closeEvent)
    }


    svgEditor.$click(this.$donot_save_close, donSaveClose)
    svgEditor.$click(this.$cancel, cancels)
    svgEditor.$click(this.$save, saves)
    this.$dialog.addEventListener('close', cancels)
  

  }
}

// Register
customElements.define('se-save-dialog', SeSave)
